<?php
/**
 * --------------------------------------------------------------
 * excel 类
 * Author: zd <kfzphp@126.com>
 * Date: 2018-06-01
 * Project: kfzphp.com
 * --------------------------------------------------------------
 */

if(APP_DEBUG) {
    ini_set("display_errors", "On");
    error_reporting(E_ALL);
}

class KfzExcel
{
    /**
     * 初始化
     * @author zd <kfzphp@126.com>
     * @since  2018-04-28
     */
    public function __construct()
    {
        $this->creator = 'zd <kfzphp@126.com>';
        $this->version = '2003';
    }

    /**
     * 导出至excel
     * @author zd <kfzphp@126.com>
     * @since  2018-04-28
     * @param  [type]     $data [description]
     * @return [type]           [description]
     */
    public function export($data)
    {
        // 判断数据
        if(empty($data)) {
            exit('excel data empty');
        }

        // 文件名称
        $excelFileName = $data['excelFileName'];
        // 表标题
        $sheetTitle = $data['sheetTitle'];
        // 表字段
        $sheetField = $data['sheetField'];

        $sheetData = $data['sheetData'];

        // 实例化类
        require_once VENDOR_PATH . '/PHPExcel/PHPExcel.php';
        require_once VENDOR_PATH . '/PHPExcel/PHPExcel/Writer/Excel2007.php';
        require_once VENDOR_PATH . '/PHPExcel/PHPExcel/Writer/Excel5.php';
        include_once VENDOR_PATH . '/PHPExcel/PHPExcel/IOFactory.php';
        $objExcel = new PHPExcel();

        // 设置属性
        $objExcel->getProperties()->setCreator($this->creator);  // 作者
        $objExcel->getProperties()->setLastModifiedBy($this->creator);  // 最后一次保存者
        $objExcel->getProperties()->setTitle($sheetTitle);     // 标题
        $objExcel->getProperties()->setSubject("Office Excel Document");    // 主题
        $objExcel->getProperties()->setDescription("Generated using PHP classes.");  // 备注
        $objExcel->getProperties()->setKeywords("Office Excel php");  // 标记
        $objExcel->getProperties()->setCategory("PHPExcel file");  // 类别
        $objExcel->setActiveSheetIndex(0);

        // 列表标题
        $letter = 'A';
        foreach ($sheetField as $v) {
            $objExcel->getActiveSheet()->setCellValue($letter++ . '1', $v['name']);
        }
        // $array = array();
        // 列表内容
        $i = 2;
        foreach($sheetData as $v) {
            $j = 'A';
            foreach($sheetField as $f) {
                // pt($v[$f['field']]);
                // $value2 = iconv("gbk","utf-8", $value2);
                $objExcel->getActiveSheet()->setCellValueExplicit($j.$i, $v[$f['field']], PHPExcel_Cell_DataType::TYPE_STRING);
                // $arr['kk'] = $j.$i;
                // $arr['vv'] = $v[$f['field']];
                // $array[] = $arr;
                $j++;
            }
            $i++;
        }
        // pt($array);
        // 列的宽度
        foreach ($sheetField as $v) {
            $objExcel->getActiveSheet()->getColumnDimension($letter++)->setWidth($v['width']);
            // $objExcel->getActiveSheet()->getColumnDimension($letter++)->setAutoSize(true);
        }

        $objExcel->getActiveSheet()->getHeaderFooter()->setOddHeader('&L&BPersonal cash register&RPrinted on &D');
        $objExcel->getActiveSheet()->getHeaderFooter()->setOddFooter('&L&B' . $objExcel->getProperties()->getTitle() . '&RPage &P of &N');

        // 设置页方向和规模
        $objExcel->getActiveSheet()->getPageSetup()->setOrientation(PHPExcel_Worksheet_PageSetup::ORIENTATION_PORTRAIT);
        $objExcel->getActiveSheet()->getPageSetup()->setPaperSize(PHPExcel_Worksheet_PageSetup::PAPERSIZE_A4);
        $objExcel->setActiveSheetIndex(0);
        $fileName = !empty($excelFileName) ? $excelFileName : date('Y-m-d H:i:s', TIMESTAMP);
        if($this->version == '2007') { //导出excel2007文档
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel2007');
            $objWriter->save('php://output');
            exit;
        } else {  //导出excel2003文档
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
        }
    }
}